@echo off
chcp 65001

set SERVER_ID="jphenix_00"
set JAVA_HOME=D:\programs\jdks\openjdk-17.0.10
set BASE_PATH=D:\java_codes
set PROJECT_NAME=gk
set PROJECT_WEB_PATH=gk_server

set RESOURCE_PATH=%BASE_PATH%\%PROJECT_NAME%\file_sources
set WEB_INF_PATH=%BASE_PATH%\%PROJECT_NAME%\%PROJECT_WEB_PATH%\WEB-INF
set KERNEL_JAR_PATH=%WEB_INF_PATH%\lib\jphenix_sdk.jar

rem set MEM_ARGS=-XX:PermSize=64m -XX:MaxPermSize=128m -Xms1024M -Xmx1024M
set DEBUG_MODE=-Xdebug -Xrunjdwp:transport=dt_socket,address=0.0.0.0:7001,server=y,suspend=n

:stop_process
set PID_PATH=%RESOURCE_PATH%\jphenix.pid
if exist %PID_PATH% (
  for /f %%i in (%PID_PATH%) do (
    taskkill /F /pid %%i > nul
  )
  del /q %PID_PATH%
)

:run_begin
if exist "%KERNEL_JAR_PATH%.new" (
  if exist "%KERNEL_JAR_PATH%.old" del /q "%KERNEL_JAR_PATH%.old"
  ren "%KERNEL_JAR_PATH%" jphenix_sdk.jar.old
  ren "%KERNEL_JAR_PATH%.new" jphenix_sdk.jar
  del /q "%RESOURCE_PATH%\script_classes"
)

%JAVA_HOME%\bin\java -jar %MEM_ARGS% %DEBUG_MODE% -Djava.awt.headless=true -Djphenix.server.id=%SERVER_ID% %KERNEL_JAR_PATH% 2 9000
if %ERRORLEVEL% NEQ 9 (
  goto done
)
goto run_begin

:done
echo "The Process Exit Value:%ERRORLEVEL%"